package com.fyakm.daodejing.simple.stockTest;

import com.fyakm.daodejing.simple.stockTest.domain.Investors;
import com.fyakm.daodejing.simple.stockTest.domain.StockEntity;
import com.fyakm.daodejing.simple.stockTest.domain.StockHistory;
import com.fyakm.daodejing.simple.stockTest.service.ProfitCalculator;
import com.fyakm.daodejing.simple.stockTest.service.TradingSimulator;
import com.fyakm.daodejing.simple.stockTest.util.CsvReader;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 测试指定股票
 */
public class TestAppointStock {

    public static void main(String[] args) {
        // 股票数据目录
        String dir = "C:\\Users\\22603\\Desktop\\temp\\A股数据";

        // 读取目录下的所有股票文件
        File directory = new File(dir);
        File[] files = directory.listFiles((dir1, name) -> name.endsWith(".csv"));

        if (files == null || files.length == 0) {
            System.out.println("目录下没有找到股票数据文件！");
            return;
        }

        // 初始化股票列表
        List<StockEntity> stockList = new ArrayList<>();
        for (File file : files) {
            String filePath = file.getAbsolutePath();
            List<StockHistory> historyList = CsvReader.readStockHistoryFromCsv(filePath);

            // 截取后100条数据
            if (historyList.size() > 20) {
                historyList = historyList.subList(historyList.size() - 20, historyList.size());
            }

            StockEntity stock = new StockEntity();
            stock.setCode(file.getName().replace(".csv", "")); // 使用文件名作为股票代码
            stock.setHistory(historyList);

            stockList.add(stock);

            System.out.println("初始化股票 " + stock.getCode() + " 完成");

            if (stockList.size() >= 20) { // 只初始化20只股票
                break;
            }
        }

        // 初始化投资人列表
        List<Investors> investorsList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) { // 初始化5个投资人
            Investors investor = new Investors();
            investor.setId(i);
            investor.setName("投资人" + i);
            investor.setMoney(1000000 * i); // 初始资金递增
            investor.setStockMap(new HashMap<>());
            investorsList.add(investor);
        }

        // 模拟交易：每个投资人投资多只股票
        for (Investors investor : investorsList) {
            for (StockEntity stock : stockList) {
                TradingSimulator.simulateTrading(investor, stock, stock.getHistory());
            }
        }

        // 创建 DecimalFormat 对象，用于格式化数字
        DecimalFormat decimalFormat = new DecimalFormat("#,###.00");

        // 计算每个投资人的总盈利
        for (Investors investor : investorsList) {
            double totalProfit = 0;
            for (StockEntity stock : stockList) {
                double profit = ProfitCalculator.calculateProfit(investor, stock);
                totalProfit += profit;

                // 格式化盈利金额
                String formattedProfit = decimalFormat.format(profit);
                System.out.println(investor.getName() + " 在 " + stock.getCode() + " 上的盈利: " + formattedProfit);
            }

            // 格式化总盈利金额
            String formattedTotalProfit = decimalFormat.format(totalProfit);
            System.out.println(investor.getName() + " 在所有股票上的总盈利: " + formattedTotalProfit);
        }
    }
}